<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/7/8
 * Time: 21:36
 */

namespace common\models\Service;


use common\models\CommonFunction;
use common\models\DAO\User;
use common\models\DAO\UserHongbaoList;

class BindService {
    public static function validate($tel, $source = false){
        $tel = CommonFunction::encrypt($tel);
        $user = User::findOne(['phone'=>$tel]);
        if($user){
            if($user['openID'] && $source)
                return ['status'=>0, 'msg'=>'该手机号已被绑定！'];
            return true;
        }
        return false;
    }

    public static function team($userInfo){
        $hongbao = 3000;
        if(!empty($userInfo['openID'])){
            $is_follow = UserHongbaoList::findOne(['followID'=>1,'userID'=>$userInfo['userID']]);
            if($is_follow)  return true;
            $result = HongBaoService::add($userInfo, $hongbao);
            if(!$result)
                return false;
            if(!empty($userInfo['recID']) && !empty($userInfo['tel'])){
                $userInfo['type'] = 4;
                $userInfo['integral'] = 40;
                $userInfo['des'] = '好友'.CommonFunction::dealTel($userInfo['tel']).'关注微信公众号';
                $result = IntegralService::addIntegral($userInfo);
                if(!$result)
                    return false;
            }
        }
        return true;

    }
}